/**
 * 登录组件
 */
define(['vue', 'text!tpl/login.html','vue-strap', 'service/utilService','service/loginService'],
    function (Vue, loginHtml,VueStrap, utilService, loginService) {

    return Vue.extend({
        template: loginHtml,
        data: function(){
            return {
                result: null,
                phoneNum: null,
                pwd: null,
                validated: true
            }
        },
        watch: {
            'phoneNum': function(val, oldVal){
                this.validated = utilService.validatePhone(val)
            }
        },
        route: {
            data: function(transition){
                var phoneNum = this.$cookie.get('phoneNum')
                if(utilService.validatePhone(phoneNum)) {
                    //alert('用户已登录')
                    this.$router.go('/home')
                    return
                }

                return {
                    phoneNum: phoneNum,
                    validated: false,
                    fromPath: transition.from.path
                }
            }
        },
        methods: {
            doSubmit: function(){
                // console.log('pwd', this.pwd)
                if(utilService.validatePhone(this.phoneNum)){
                    if (loginService.login(this.phoneNum, this.pwd)) {
                        this.$cookie.set('phoneNum', this.phoneNum, 1);
                        this.$router.go('/home')
                    }
                } else {
                    alert('请输入正确的手机号')
                }
            }
        }
    })
})
